package com.example.pruebaHttp;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import lolapi.dto.Summoner.Summoner;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.datas.Comunicador;
import com.example.datas.SummonerData;

public class DatosPagina extends Activity {
	private TextView summonerName;
	private TextView summonerRank;
	private TextView summonerLevel;
	private ImageView summonerIcon;
	private ImageView summonerRankImage;
	private SummonerData summonerData;
	String iconURL;
	Map<String, Summoner> summoners;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datos_pagina);
		Display display = getWindowManager().getDefaultDisplay();
		int width = (int) (0.15 * display.getWidth()); 
		summonerData = Comunicador.getCommunicador();
		summonerName = (TextView) findViewById(R.id.summonerName);
		summonerRank = (TextView) findViewById(R.id.summonerLeague);
		//AWP = (TextView) findViewById(R.id.AWP);
		summonerLevel = (TextView) findViewById(R.id.summonerLevel);
		summonerIcon = (ImageView) findViewById(R.id.imageView1);
		ImageView bg = (ImageView) findViewById(R.id.bg);
		//ImageView infoBG = (ImageView) findViewById(R.id.infoBG);
		summonerRankImage = (ImageView) findViewById(R.id.rankImage);
		iconURL = "http://ddragon.leagueoflegends.com/cdn/"
				+ summonerData.getDDVersion() + "/img/profileicon/"
				+ summonerData.getSummonerIcon() + ".png";
		// Scaling summonerIcon.
		LayoutParams parms = summonerIcon.getLayoutParams();
		parms.height = width;
		parms.width = width;
		summonerIcon.setLayoutParams(parms);
		// Scaling BG
		parms = bg.getLayoutParams();
		parms.height = display.getHeight();
		parms.width = display.getWidth();
		bg.setLayoutParams(parms);
		// Scaling summonerRankImage
		parms = summonerRankImage.getLayoutParams();
		parms.width = (int) (0.25 * display.getWidth());
		parms.height = parms.width;
		summonerRankImage.setLayoutParams(parms);
		TareaWSConsulta tarea = new TareaWSConsulta();
		tarea.execute();
		
		((TextView) findViewById(R.id.totalNormalWins)).setText(""+summonerData.getNormalWins());;
		((TextView) findViewById(R.id.normalKA)).setText(""+summonerData.getNormalKA());
		TextView wardsPlaced = ((TextView) findViewById(R.id.averageWP));
		// put the wards placed color.
		if (summonerData.getAverageWardsPlaced() < 5) {
			// Menos de 5 wards puestos.
			wardsPlaced.setTextColor(Color.RED);
		} else {
			if (summonerData.getAverageWardsPlaced() < 10) {
				// Entre 5 y 10 wards.
				wardsPlaced.setTextColor(Color.parseColor("#088A08"));

			} else {
				if (summonerData.getAverageWardsPlaced() < 20) {
					// entre 10 y 20.
					wardsPlaced.setTextColor(Color.BLUE);

				} else {
					// mas de 20 wards.
					wardsPlaced.setTextColor(Color.MAGENTA);

				}
			}
		}
		wardsPlaced.setText(""+summonerData.getAverageWardsPlaced());

		TextView wardsKilled = ((TextView) findViewById(R.id.averageWK));
		// put the wards placed color.
		if (summonerData.getAverageWardsKilled() < 1) {
			// Menos de 5 wards puestos.
			wardsKilled.setTextColor(Color.RED);
		} else {
			if (summonerData.getAverageWardsKilled() < 3) {
				// Entre 5 y 10 wards.
				wardsKilled.setTextColor(Color.parseColor("#088A08"));

			} else {
				if (summonerData.getAverageWardsKilled() < 8) {
					// entre 10 y 20.
					wardsKilled.setTextColor(Color.BLUE);

				} else {
					// mas de 20 wards.
					wardsKilled.setTextColor(Color.MAGENTA);
				}
			}
		}
		wardsKilled.setText(""+summonerData.getAverageWardsKilled());
		TextView mostCC = (TextView) findViewById(R.id.mostCC);
		mostCC.setText(""+mostCC.getText()+summonerData.getMostCC());
		TextView bestCrit= (TextView) findViewById(R.id.bestCrit);
		bestCrit.setText(""+bestCrit.getText()+summonerData.getBestCrit());
		TextView averageCC = (TextView) findViewById(R.id.averageCC);
		averageCC.setText(""+averageCC.getText()+summonerData.getAverageCC());
		TextView numVictories = (TextView) findViewById(R.id.numVictories);
		numVictories.setText(""+numVictories.getText()+summonerData.getLastWins());
		TextView averageCS = (TextView) findViewById(R.id.averageCS);
		averageCS.setText(""+averageCS.getText()+summonerData.getAverageCS());
		TextView mostTurrets = (TextView) findViewById(R.id.mostTurretsKilled);
		mostTurrets.setText(""+mostTurrets.getText()+summonerData.getMostTurretsKilled()+" ");
		summonerName.setText(summonerData.getSummonerName());
		summonerLevel.setText("" + summonerData.getSummonerLevel());
		summonerRank.setText(" "+summonerData.getSummonerRank()+" ");
		if (summonerData.getSummonerRank().contains("BRONZE"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.bronze_6));
		if (summonerData.getSummonerRank().contains("SILVER"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.silver_6));
		if (summonerData.getSummonerRank().contains("GOLD"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.gold_6));
		if (summonerData.getSummonerRank().contains("PLATINUM"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.platinum_6));
		if (summonerData.getSummonerRank().contains("DIAMOND"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.diamond_6));
		if (summonerData.getSummonerRank().contains("CHALLENGER"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.challenger_1));
		if (summonerData.getSummonerRank().contains("UNRANKED"))
			summonerRankImage.setImageDrawable(getResources().getDrawable(
					R.drawable.unrankedlol));
	}

	private class TareaWSConsulta extends AsyncTask<String, Integer, Boolean> {
		Bitmap bitmap;

		protected Boolean doInBackground(String... params) {
			boolean resul = true;
			try {
				bitmap = BitmapFactory.decodeStream((InputStream) new URL(
						iconURL).getContent());
			}

			catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return resul;
		}

		protected void onPostExecute(Boolean result) {
			summonerIcon.setImageBitmap(bitmap);
		}

	}
}